/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zorlog.ui.graph;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.image.BufferedImage;
import java.util.List;


enum LegendType {
	VERT,
	HORIZ
}

enum LegendAlignX {
	LEFT,
	CENTER,
	RIGHT
}


/**
 * Draws a buffered legend using alpha
 */
abstract class Legend {
	public static Legend create(LegendType type, LegendAlignX alignX, Font font, List<Graph> graphs) {
		switch (type) {
			case VERT:	
				return new VertLegend(alignX, font, graphs);
			case HORIZ:
				return new HorizLegend(alignX, font, graphs);
			default:
				return null;
		}
	}
	
	protected Legend(LegendAlignX alignX, Font font, List<Graph> graphs) {
		this.alignX = alignX;
		this.font = font;
		this.graphs = graphs;
	}

	public final void paint(Graphics g) {
		if (buffer == null)
			build();
		
		int x = 0;
		
		switch (alignX) {
			case LEFT:
				x = 0;
				break;
			case RIGHT:
				x = g.getClipBounds().width - buffer.getWidth();
				break;
			case CENTER:
				x = (g.getClipBounds().width - buffer.getWidth()) / 2;
				break;
		}		
		
		g.drawImage(buffer, x, 0, null);
	}

	protected abstract void draw(Graphics g);
	protected abstract Dimension getDimension();	
	
	private void build() {
		Dimension d = getDimension();
			
		buffer = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = buffer.createGraphics();
		g.setFont(font);
		
		g.setColor(new Color(0xC0FFFFFF, true));
		g.fillRect(0, 0, d.width - 1, d.height - 1);
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, d.width - 1, d.height - 1);

		draw(g);
	}
	
	private final LegendAlignX alignX;
	protected final Font font;
	protected final List<Graph> graphs;
	
	private BufferedImage buffer = null;	
}


class VertLegend extends Legend {
	protected VertLegend(LegendAlignX alignX, Font font, List<Graph> graphs) {
		super(alignX, font, graphs);
	}
	
	@Override
	protected void draw(Graphics g) {
		int x = 4;
		int y = font.getSize() + 4;
		
		for (Graph graph : graphs) {
			g.setColor(graph.getColor());
			g.drawString(graph.getName(), x, y);
			y += font.getSize();
		}
	}

	@Override
	protected Dimension getDimension() {
		FontRenderContext frc = new FontRenderContext(null, false, false);
		int w = 0;
		int h = 0;
		
		for (Graph graph : graphs) {
			w = Math.max(w, (int)font.getStringBounds(graph.getName(), frc).getWidth());
			h += font.getSize();
		}
		
		return new Dimension(w + 8, h + 8);
	}
}


class HorizLegend extends Legend {
	protected HorizLegend(LegendAlignX alignX, Font font, List<Graph> graphs) {
		super(alignX, font, graphs);
	}
	
	@Override
	protected void draw(Graphics g) {
		FontRenderContext frc = new FontRenderContext(null, false, false);
		int x = 4;
		int y = font.getSize() + 4;
		
		for (Graph graph : graphs) {
			g.setColor(graph.getColor());
			g.drawString(graph.getName(), x, y);
			x += (int)font.getStringBounds(graph.getName(), frc).getWidth() + 8;
		}
	}

	@Override
	protected Dimension getDimension() {
		FontRenderContext frc = new FontRenderContext(null, false, false);
		int w = 0;
		int h = font.getSize();
		
		for (Graph graph : graphs) {
			if (w != 0)
				w += 8;
			w += (int)font.getStringBounds(graph.getName(), frc).getWidth();
		}

		return new Dimension(w + 8, h + 8);
	}
}

